local skill = fk.CreateSkill {
  name = "3s__shijing",
}

Fk:loadTranslationTable{
  ["3s__shijing"] = "释经",
  [":3s__shijing"] = "回合结束时，你可弃置一张手牌，获得弃牌堆内任意张牌名字数之和为X且不同名的牌（X为此牌因”治心“被保留的次数，若本回合因”治心“结束，X视为4）。",
  ["$3s__shijing1"] = "经义深奥，了然于胸。",
  ["$3s__shijing2"] = "潜心研读，顿悟禅机。",
  ["#3s__shijing-discard"] = "释经：请弃置一张手牌",
  ["#3s__shijing-select"] = "释经：请选择弃牌堆中的牌（名字数之和：%arg）",
  ["discard_pile"] = "弃牌堆",
}

local function CountNameNum(card)
  if card.name == "lvshi__blade" then
    return 3
  elseif card.name == "lvshi__crossbow" then
    return 2
  end
  return Fk:translate(card.trueName, "zh_CN"):len()
end

Fk:addPoxiMethod{
  name = "3s__shijing",
  card_filter = function(to_select, selected, data, extra_data)
    if #selected >= extra_data.X then return end -- 最多选2张
    local card = Fk:getCardById(to_select)
    local n = CountNameNum(card)
    local sum = 0
    if #selected == 0 then
      return n <= extra_data.X
    else
      for _, id in ipairs(selected) do
        sum = sum + CountNameNum(Fk:getCardById(id))
        if Fk:getCardById(id).trueName == card.trueName then
          return false
        end
      end
      return n + sum <= extra_data.X
    end
  end,
  feasible = function(selected, data, extra_data)
    local sum = 0
    for _, id in ipairs(selected) do
      sum = sum + CountNameNum(Fk:getCardById(id))
    end
    return sum == extra_data.X
  end,
  prompt = function(data, extra_data)
    return extra_data.prompt
  end,
  default_choice = function(data, extra_data)
    local cards = data[1][2] -- 手牌列表
    local id = table.find(cards, function(c) return CountNameNum(Fk:getCardById(c)) == extra_data.X end)
    if id then
      return {id}
    end
    return {} -- 找不到则返回空
  end
}

skill:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skill.name) and player:getHandcardNum() > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 询问是否弃置一张手牌来发动技能
    local discard = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = skill.name,
      pattern = ".",
      prompt = "#3s__shijing-discard",
      skip = true,
    })
    if #discard > 0 then
      event:setCostData(self, { discarded = discard })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local discarded = event:getCostData(self).discarded
    -- 计算X值
    local card = Fk:getCardById(discarded[1])
    local X = card:getMark("@zhixin-inhand") or 0
    -- 检查是否因治心结束回合
    if player:getMark("3s__zhixin_end") > 0 then
      X = 4
    end
    room:throwCard(discarded, skill.name, player, player)
    --[[local cards = {}
    for _, id in ipairs(room.discard_pile) do
      local card = Fk:getCardById(id)
      if CountNameNum(card) <= X and table.every(cards, function(c) return Fk:getCardById(c).name ~= card.name end) then
        table.insert(cards, card)
      end
    end]]
    local cards_data = {{"discard_pile", room.discard_pile}}
    local selected_cards = room:askToPoxi(target, {
      poxi_type = "3s__shijing",
      data = cards_data,
      extra_data = {
        skillName = skill.name,
        prompt = "#3s__shijing-select:::"..X,
        X = X,
      },
      cancelable = true,
    })
    if #selected_cards > 0 then
      room:moveCardTo(selected_cards, Card.PlayerHand, player, fk.ReasonJustMove, skill.name, nil, true, player)
    end
  end,
})

return skill